using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class app_frame_Default2 : System.Web.UI.Page
{
    DataSet dsWorkflow = new DataSet();

    HDDatabase HDD = null;
    string sUTC_Offset = "0";
    protected void Page_PreInit(object sender, EventArgs e)
    {
        this.Theme = Session["Theme"].ToString();
        this.Title = "MaxBlox";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        security hdds = new security();
        if (Request.QueryString["mn"] != null)
            hdds.logMenuClick(Request.QueryString["mn"].ToString());

        if (Session["userid"] == null) Response.Redirect(hdds.SessionExpiryURL);
        if (Session["userid"].ToString() == "") Response.Redirect(hdds.SessionExpiryURL);

        //Code added by Sampath on 18-Nov-2011 for IPP Vertical privilege
        if (Request.QueryString["mn"] != null)
        {
            if (!hdds.checkObjectMenuAccess(Request.QueryString["mn"]))
            {
                tblcontrols.Visible = false;
                lblmessage.Text = wrapItAsMessage("<img src=\"Images/info.gif\" alt=\"\"></img>&nbsp;You do not have sufficient access permission.", true);
                return;
            }
        }

        HDD = new HDDatabase();
        loadDataset();
        getHistorytoShow();
        loadCombos();

        string sNoofRecords = this.HDD.getAcrMemValue("3813ac7202378893", "1"); // added by Jeya Prabha on 17-Sep-2007 to set the number of records displayed
        if (sNoofRecords != "" && sNoofRecords != "0")
            GlobalValues.intNoOfRecordsPerPage = int.Parse(sNoofRecords);
        else
            GlobalValues.intNoOfRecordsPerPage = 8;

        DynamicDisplayI DDI = new DynamicDisplayI();
        sUTC_Offset = DDI.getUserSetting("sy_usr", "time_zone", "sy_timezone", "utc_offset", "timezone_id");
        DDI = null;
        
        if (IsPostBack)
        {
            ddlRuleset.SelectedValue = Request.Form["ctl00$ContentPlaceHolder3$ddlRuleset"];
            ddlStatus.SelectedValue = Request.Form["ctl00$ContentPlaceHolder3$ddlStatus"];
        }
        getpageicons();// Added by srivatsan
        loadHistory(dsWorkflow.Tables["sy_workflow_history"]);
        //loadHistory(getFilteredTable());//loadHistory(dsWorkflow.Tables["History"]);
    }

    private string getUserDisplayName()
    {
        string userDisplayField = HDD.GetColumnValue("sy_acr_mem", "mem_id", "af_row_id='30583eb791cb7d5b'");
        userDisplayField = userDisplayField.Trim().Length == 0 ? "user_id" : userDisplayField;
        return userDisplayField;
    }

    private void loadDataset()
    {
        string sSql = "Select * from sy_workflow_ruleset";
        dsWorkflow.Tables.Add(HDD.GetDataTable(sSql, "sy_workflow_ruleset").Copy());
        sSql = "Select * from sy_workflow_rules";
        dsWorkflow.Tables.Add(HDD.GetDataTable(sSql, "sy_workflow_rules").Copy());
        //sSql = "Select * from sy_workflow_history";
        //dsWorkflow.Tables.Add(HDD.GetDataTable(sSql, "sy_workflow_history").Copy());
        string sUserDisplayName = getUserDisplayName();
        sSql = "select *, " + sUserDisplayName + " as display_name from sy_usr";
        dsWorkflow.Tables.Add(HDD.GetDataTable(sSql, "sy_usr").Copy());
        sSql = "select * from sy_org_structure";
        dsWorkflow.Tables.Add(HDD.GetDataTable(sSql, "sy_org_structure").Copy());
        sSql = "Select * from sy_obj";
        dsWorkflow.Tables.Add(HDD.GetDataTable(sSql, "sy_obj").Copy());
        sSql = "Select distinct(status) as TextField, status as ValueField from sy_workflow_history";
        dsWorkflow.Tables.Add(HDD.GetDataTable(sSql, "status").Copy());
        sSql = "Select * from sy_workitem_user";
        dsWorkflow.Tables.Add(HDD.GetDataTable(sSql, "sy_workitem_user").Copy());
    }

    private void loadCombos()
    {
        /*ddlRuleset.Items.Clear();
        DataRow dr = dsWorkflow.Tables["sy_workflow_ruleset"].NewRow();
        dr["af_row_id"] = "";
        dr["name"] = "--All--";
        dsWorkflow.Tables["sy_workflow_ruleset"].Rows.InsertAt(dr, 0);
        ddlRuleset.DataSource = dsWorkflow.Tables["sy_workflow_ruleset"];
        ddlRuleset.DataTextField = "name";
        ddlRuleset.DataValueField = "af_row_id";
        ddlRuleset.DataBind();*/

        ddlStatus.Items.Clear();
        DataRow dr = dsWorkflow.Tables["status"].NewRow();
        dr["TextField"] = "--All--";
        dr["ValueField"] = "";
        dsWorkflow.Tables["status"].Rows.InsertAt(dr, 0);

        DataRow[] drr = dsWorkflow.Tables["status"].Select("", "TextField asc");
        foreach (DataRow drrr in drr)
            ddlStatus.Items.Add( new ListItem(drrr["TextField"].ToString(),drrr["ValueField"].ToString()));
    }

    private string getWorkItemForUser()
    {
        string sRet = "";
        try
        {
            DataRow[] drr = dsWorkflow.Tables["sy_workitem_user"].Select("for_user='" + Session["user_afrowid"].ToString() + "'");

            foreach (DataRow dr in drr)
                sRet = sRet + "'" + dr["af_row_id"].ToString() + "',";

            sRet = sRet.TrimEnd(',');
        }

        catch (Exception ex)
        {
            LogWriter.WriteLog("getForUser : Error : " + ex.Message);
        }
        return sRet;
    }

    private string getNumberOfRecords()
    {
        int iTotalRecords=0;
        try{
            int iRemaining = 0;
            iRemaining = int.Parse(hf_records.Value)%GlobalValues.intNoOfRecordsPerPage;
            int iTotalPages = 0;//getTotalPages(frmObj.Controls, ref iRemaining);
            iTotalPages = int.Parse(hf_Total.Value);
            //int iCurrPage = getCurrentPage(frmObj.Controls);
            int iCurrPage = int.Parse(hf_Count.Value);
            int iRecordCount = int.Parse(hf_records.Value);
            if (iCurrPage > iTotalPages)
                iCurrPage = 0;
            if (iTotalPages == 0)
            {
                iTotalRecords = iRemaining;
            }
            else if (iCurrPage == iTotalPages)
            {
                if (iRemaining == 0)
                {
                    iTotalRecords = GlobalValues.intNoOfRecordsPerPage;
                }
                else
                {
                    iTotalRecords = iRemaining;
                }
            }
            else if (iCurrPage == 0)
            {
                iTotalRecords = iRecordCount;
            }
            else
            {
                iTotalRecords = iRecordCount - (iCurrPage * GlobalValues.intNoOfRecordsPerPage);
                iTotalRecords = ((iTotalPages - iCurrPage) * GlobalValues.intNoOfRecordsPerPage) + iRemaining;
            }
        }

        catch{
        }
        return iTotalRecords.ToString();
    }

    private void getHistorytoShow()
    {
        try
        {
            if (!IsPostBack)
            {
                ddlRuleset.Items.Clear();
                ddlRuleset.Items.Add(new ListItem("--All--", ""));
            }

            //string sCurrentUserTitle = getColumnValue("sy_usr", "af_row_id", Session["user_afrowid"].ToString(), "title");
            //sCurrentUserTitle = getColumnValue("sy_org_structure", "af_row_id", sCurrentUserTitle, "title");

            //DataRow[] drr = null;

            //if (Session["user_afrowid"].ToString() == "650a333596bbf348")
            //{
            //    drr = dsWorkflow.Tables["sy_workflow_history"].Select("", "created_at desc");
            //}
            //else
            //{
            //    string sUser_Workitem = getWorkItemForUser();
            //    if (sUser_Workitem == "")
            //        drr = dsWorkflow.Tables["sy_workflow_history"].Select("source_title='" + sCurrentUserTitle.Replace("'", "''") + "' or target_title='" + sCurrentUserTitle.Replace("'", "''") + "' or created_by='" + Session["user_afrowid"] + "'");
            //    else
            //        drr = dsWorkflow.Tables["sy_workflow_history"].Select("source_title='" + sCurrentUserTitle.Replace("'", "''") + "' or target_title='" + sCurrentUserTitle.Replace("'", "''") + "' or created_by='" + Session["user_afrowid"] + "' or af_row_id in (" + sUser_Workitem + ")");
            //}
            string sFilter = queryForTitle();
            string sCondition = queryForQuickSearch();
            string sSql = "select count (*) from sy_workflow_history ";
            if (sFilter != "")
            {
                sSql += " where (" + sFilter + ") ";
                if (sCondition != "")
                    sSql += " and " + sCondition;
            }
            else
            {
                if (sCondition != "")
                    sSql += " where " + sCondition;
            }
            
            hf_records.Value = HDD.GetColumnValue(sSql);
            int iCount = int.Parse(hf_records.Value) / GlobalValues.intNoOfRecordsPerPage;
            hf_Total.Value = iCount.ToString();

            sSql = "select top " + getNumberOfRecords() + " * from sy_workflow_history";
            if (sFilter != "")
            {
                sSql += " where (" + sFilter + ") ";
                if (sCondition != "")
                    sSql += " and " + sCondition;
            }
            else
            {
                if (sCondition != "")
                    sSql += " where " + sCondition;
            }

            sSql += " order by created_at asc";

            sSql = "Select top " + GlobalValues.intNoOfRecordsPerPage.ToString() + " * from ( " + sSql + ") as tt order by " + "created_at" + " " + "desc";

            dsWorkflow.Tables.Add(HDD.GetDataTable(sSql, "sy_workflow_history").Copy());

            //DataTable dtHistory = dsWorkflow.Tables["sy_workflow_history"].Clone();
            //dtHistory.TableName = "History";
            //DataTable dtRulesetName = new DataTable("Ruleset Name");
            //DataColumn dc = new DataColumn("Ruleset Name");
            //dtRulesetName.Columns.Add(dc);
            //dc = new DataColumn("RulesetId");
            //dtRulesetName.Columns.Add(dc);
            ////DataTable dtStatus = new DataTable("Status");
            ////dc = new DataColumn("Status");
            ////dtStatus.Columns.Add(dc);
            ////dc.Dispose();
            ////dc = null;
            //if (dsWorkflow.Tables.Contains("Ruleset Name")) dsWorkflow.Tables.Remove("Ruleset Name");
            //dsWorkflow.Tables.Add(dtRulesetName);
            ////dsWorkflow.Tables.Add(dtStatus);
            //DataRow drTemp = null;
            //foreach (DataRow dr in dsWorkflow.Tables["sy_workflow_history"].Rows)//foreach (DataRow dr in drr)
            //{
            //    if (getColumnValue("Ruleset Name", "RulesetId", dr["ruleset_id"].ToString(), "Ruleset Name") == "")  //if (ddlRuleset.Items.FindByValue(dr["ruleset_id"].ToString()) == null)
            //    {
            //        string sTemp = getColumnValue("sy_workflow_ruleset", "af_row_id", dr["ruleset_id"].ToString(), "name");
            //        if (sTemp != "")
            //        {
            //            drTemp = dtRulesetName.NewRow();
            //            drTemp["Ruleset Name"] = sTemp;
            //            drTemp["RulesetId"] = dr["ruleset_id"].ToString();
            //            dtRulesetName.Rows.Add(drTemp);
            //            //ddlRuleset.Items.Add(new ListItem(sTemp, dr["ruleset_id"].ToString()));

            //            if (dtHistory.Select("ruleset_id='" + dr["ruleset_id"].ToString() + "'").Length == 0)
            //                getTableFromArray(ref dtHistory, dsWorkflow.Tables["sy_workflow_history"].Select("ruleset_id='" + dr["ruleset_id"].ToString() + "'"));
            //        }
            //    }
            //}

            if (!IsPostBack)
            {

                sSql = "SELECT DISTINCT sy_workflow_rules.ruleset, sy_workflow_ruleset.name " +
                        " FROM         sy_workflow_rules INNER JOIN " +
                        "sy_workflow_ruleset ON sy_workflow_rules.ruleset = sy_workflow_ruleset.af_row_id INNER JOIN " +
                        "sy_workflow_history ON sy_workflow_rules.ruleset = sy_workflow_history.ruleset_id ";
                if (sFilter != "")
                {
                  sSql += "WHERE " + sFilter.Replace("source_title in", " sy_workflow_history.source_title IN ").Replace("target_title in ", "sy_workflow_history.target_title IN ");
                }

                sSql += " order by [sy_workflow_ruleset].[name] asc";
                        //(sy_workflow_history.source_title IN (" + sFilter + " )) OR " +
                        //" (sy_workflow_history.target_title IN (" + sFilter + " ))";
                ddlRuleset.DataSource = HDD.GetDataTable(sSql, "Ruleset Name");
                ddlRuleset.DataTextField = "name";
                ddlRuleset.DataValueField = "ruleset";
                ddlRuleset.DataBind();
                ddlRuleset.Items.Insert(0, new ListItem("--All--", ""));
            }

            
            

            //if (!IsPostBack)
            //{
            //    DataRow[]  drr = dtRulesetName.Select("", "Ruleset Name asc");
            //    foreach (DataRow drrr in drr)
            //        ddlRuleset.Items.Add(new ListItem(drrr["Ruleset Name"].ToString(), drrr["RulesetId"].ToString()));
            //    dtRulesetName.Dispose();
            //    dtRulesetName = null;
            //}
            //DataTable dtTemp = dtHistory.Clone();
            //getTableFromArray(ref dtTemp, dtHistory.Select("", "created_at desc"));
            //dtHistory.Dispose();
            //dtHistory = null;
            //if (dsWorkflow.Tables.Contains(dtTemp.TableName)) dsWorkflow.Tables.Remove(dtTemp.TableName);
            //dsWorkflow.Tables.Add(dtTemp);
            //dtTemp.Dispose();
            //dtTemp = null;
        }

        catch (Exception ex)
        {
            LogWriter.WriteLog("getHistorytoShow : Error : " + ex.Message);
        }
    }

    protected void Page_UnLoad(object sender, EventArgs e)
    {
        dsWorkflow.Dispose();
        dsWorkflow = null;
        HDD = null;
    }

    private void loadHistory(DataTable dtHistory)
    {
        try
        {
            Table1.Rows.Clear();
            loadHeader();
            DynamicDisplayI DDI = new DynamicDisplayI();
            //int iCount = dtHistory.Rows.Count / GlobalValues.intNoOfRecordsPerPage;
            //hf_Total.Value = iCount.ToString();
            int iRowCount = 0;
            int iCountValue = int.Parse(hf_Count.Value);
            //iRowCount = iCountValue * GlobalValues.intNoOfRecordsPerPage;
            //for (; iRowCount < iCountValue * GlobalValues.intNoOfRecordsPerPage + GlobalValues.intNoOfRecordsPerPage && iRowCount < dtHistory.Rows.Count; iRowCount++)
            for (iRowCount = 0; iRowCount < dtHistory.Rows.Count; iRowCount++)
            {
                TableRow tr = new TableRow();
                getRow(dtHistory.Rows[iRowCount], ref tr, DDI);
                int iRow = iRowCount % 2;
                tr.CssClass = "Grid_Row" + iRow.ToString();
                tr.Height = Unit.Parse("20");
                tr.HorizontalAlign = HorizontalAlign.Left;
                Table1.Rows.Add(tr);
            }
            DDI = null;
            if (Table1.Rows.Count == 1)
            {
                //addNoRecordsRow();
                addRow("No Workitems available", "Grid_Row1");
            }
            
            addRow("", "displaygridbottom");
            if (int.Parse(hf_records.Value) > GlobalValues.intNoOfRecordsPerPage)
                addFooterTable(iCountValue * GlobalValues.intNoOfRecordsPerPage, iRowCount, int.Parse(hf_records.Value));
        }

        catch (Exception ex)
        {
            LogWriter.WriteLog("loadTable : Error : " + ex.Message);
        }
    }

    private void addFooterTable(int iStart, int iEnd, int iTotal)
    {
        try
        {
            Table tbFooter = new Table();
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            //string sImageURL = "Images/navi_disabled.gif";
            tbFooter.Width = Unit.Percentage(100);
            
            //if (iTotal - iEnd < 20) Table1.Rows[iTotal - iEnd + 1].Cells[0].HorizontalAlign = HorizontalAlign.Left;
            if (iStart + iEnd <= iTotal)
                iEnd = iStart + iEnd;
            iStart = iStart + 1;
            Literal lbStatus = new Literal();
            lbStatus.ID = "lbStatus";
            lbStatus.Text = "Showing " + iStart.ToString() + " to " + iEnd.ToString() + " of " + iTotal.ToString();
            tc.Controls.Add(lbStatus);
            tc.Attributes.Add("style", "font-size: 11px; font-family: Verdana");
            tr.Cells.Add(tc);

            tc = new TableCell();
            ImageButton img = new ImageButton();
            img.ID = "first";
            img.AlternateText = "First";
            if (iStart == 1)
            {
                img.ImageUrl = "Images/first_disabled.gif";
                img.Enabled = false;
            }
            else
                img.ImageUrl = "Images/first.gif";
            img.OnClientClick = "First();";
            //img.Click += new ImageClickEventHandler(img_FirstClick);
            tc.Controls.Add(img);
            tr.Cells.Add(tc);
            
            //tc = new TableCell();
            img = new ImageButton();
            img.ID = "previous";
            img.AlternateText = "Previous";
            if (iStart == 1)
            {
                img.ImageUrl = "Images/back_disabled.gif";
                img.Enabled = false;
            }
            else
                img.ImageUrl = "Images/back.gif";
            img.OnClientClick = "Previous();";
            //img.Click += new ImageClickEventHandler(img_PreviousClick);
            tc.Controls.Add(img);
            tr.Cells.Add(tc);

            //tc = new TableCell();
            img = new ImageButton();
            img.ID = "next";
            img.AlternateText = "Next";
            if (iTotal == iEnd)
            {
                img.ImageUrl = "Images/front_disabled.gif";
                img.Enabled = false;
            }
            else
                img.ImageUrl = "Images/front.gif";
            img.OnClientClick = "Next();";
            //img.Click += new ImageClickEventHandler(img_NextClick);
            tc.Controls.Add(img);
            tr.Cells.Add(tc);

            //tc = new TableCell();
            img = new ImageButton();
            img.ID = "last";
            img.AlternateText = "Last";
            if (iTotal == iEnd)
            {
                img.ImageUrl = "Images/last_disabled.gif";
                img.Enabled = false;
            }
            else
                img.ImageUrl = "Images/last.gif";
            img.OnClientClick = "Last();";
            //img.Click += new ImageClickEventHandler(img_LastClick);
            tc.Controls.Add(img);
            tc.HorizontalAlign = HorizontalAlign.Right;
            tr.Cells.Add(tc);
            tr.CssClass = "displaygridbottom";
            tbFooter.Rows.Add(tr);
            Table1.Rows[Table1.Rows.Count - 1].Cells[Table1.Rows[Table1.Rows.Count - 1].Cells.Count - 1].Controls.Add(tbFooter);
            //Table1.Rows[Table1.Rows.Count - 1].Cells[Table1.Rows[Table1.Rows.Count - 1].Cells.Count - 1].HorizontalAlign = HorizontalAlign.Right;
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("addFooterTable : Error : " + ex.Message);
        }
    }

    void img_LastClick(object sender, ImageClickEventArgs e)
    {
        int iCount = int.Parse(hf_Total.Value);
        iCount = iCount / 20;

        hf_Count.Value = iCount.ToString();
    }

    void img_NextClick(object sender, ImageClickEventArgs e)
    {
        int iCount = int.Parse(hf_Count.Value);
        if (iCount > 0)
            iCount = iCount + 1;
        else
            iCount = 1;
        hf_Count.Value = iCount.ToString();        
    }

    void img_PreviousClick(object sender, ImageClickEventArgs e)
    {
        int iCount = int.Parse(hf_Count.Value);
        if (iCount > 0)
            iCount = iCount - 1;
        else
            iCount = 0;
        hf_Count.Value = iCount.ToString();
    }

    void img_FirstClick(object sender, ImageClickEventArgs e)
    {
        hf_Count.Value = "0";
    }

    private void loadHeader()
    {
        TableRow tr = new TableRow();
        tr.Cells.Add(getCell("&nbsp;Ruleset"));
        tr.Cells.Add(getCell("Rule"));
        tr.Cells.Add(getCell("Object"));
        tr.Cells.Add(getCell("Status"));
        tr.Cells.Add(getCell("Comments"));
        tr.Cells.Add(getCell("Created By"));
        tr.Cells.Add(getCell("Created On"));
        tr.CssClass = "Grid_Header";
        tr.HorizontalAlign = HorizontalAlign.Left;
        Table1.Rows.Add(tr);
    }

    private TableCell getCell(string Value)
    {
        if (Value == "") Value = "&nbsp;";

        TableCell tc = new TableCell();
        tc.Text = Value;
        tc.HorizontalAlign = HorizontalAlign.Left;
        return tc;
    }

    private void getRow(DataRow dr, ref TableRow tr, DynamicDisplayI DDI)
    {
        try
        {
            string sTemp = getColumnValue("sy_workflow_ruleset", "af_row_id", dr["ruleset_id"].ToString(), "name");
            tr.Cells.Add(getCell("&nbsp;" + (sTemp == "" ? "No matching ruleset is available." : sTemp)));
            //sTemp == "" ? "-" : sTemp
            sTemp = getColumnValue("sy_workflow_rules", "af_row_id", dr["rule_id"].ToString(), "name");
            tr.Cells.Add(getCell(sTemp == "" ? "-" : sTemp));
            sTemp = getColumnValue("sy_obj", "af_row_id", dr["object_id"].ToString(), "object_name");
            tr.Cells.Add(getCell(sTemp == "" ? "-" : sTemp));
            tr.Cells.Add(getCell(dr["status"].ToString()));
            tr.Cells.Add(getCell(dr["comments"].ToString()));
            sTemp = getColumnValue("sy_usr", "af_row_id", dr["created_by"].ToString(), "display_name");
            //sTemp = getColumnValue("sy_usr", "af_row_id", dr["created_by"].ToString(), "user_id");
            tr.Cells.Add(getCell(sTemp == "" ? "-" : sTemp));
            Localization li = new Localization();
            string sValue = li.getDateTime4TimeZone(dr["created_at"].ToString(), sUTC_Offset, false, true);
            li = null;
            tr.Cells.Add(getCell(sValue));
        }

        catch (Exception ex)
        {
            LogWriter.WriteLog("getRow : Error : " + ex.Message);
        }
    }

    private string getColumnValue(string Tablename, string whereFieldname, string Value, string Fieldname)
    {
        string sValue = "";
        try
        {
            if (dsWorkflow.Tables.Contains(Tablename))
            {
                DataRow[] drr = dsWorkflow.Tables[Tablename].Select(whereFieldname + "='" + Value + "'");
                if (drr.Length > 0)
                    sValue = drr[0][Fieldname].ToString();
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("getColumnValue : Error :" + ex.Message);
        }
        return sValue;
    }
    protected void ddlRuleset_SelectedIndexChanged(object sender, EventArgs e)
    {
        Table1.Rows.Clear();
        hf_Count.Value = "0";
        loadHistory(dsWorkflow.Tables["sy_workflow_history"]);
        //loadHistory(getFilteredTable());
    }
    protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        Table1.Rows.Clear();
        hf_Count.Value = "0";
        loadHistory(dsWorkflow.Tables["sy_workflow_history"]);
        //loadHistory(getFilteredTable());
    }
    private DataTable getFilteredTable()
    {
        DataTable dtHistory = dsWorkflow.Tables["History"].Clone();

        try
        {
            //getHistorytoShow();
            string sCondition = "";
            if (ddlRuleset.SelectedValue != "")
                sCondition = " ruleset_id='" + ddlRuleset.SelectedValue + "'";
            if (ddlStatus.SelectedValue != "" && ddlStatus.SelectedValue != "--All--")
            {
                if (sCondition != "")
                    sCondition += " and ";
                sCondition += "status='" + ddlStatus.SelectedValue + "'";
            }
            DataRow[] drr = dsWorkflow.Tables["History"].Select(sCondition, "created_at desc");

            if (drr.Length > 0)
            {
                getTableFromArray(ref dtHistory, drr);
            }
        }

        catch (Exception ex)
        {
            LogWriter.WriteLog("getFilteredTable : Error :" + ex.Message);
        }

        return dtHistory;
    }

    private void addFooter()
    {
        TableRow htr = new TableRow();
        htr.Cells.Add(getCell("&nbsp;"));
        htr.Cells[0].ColumnSpan = 7;
        htr.Attributes.Add("Class", "displaygridbottom");
        Table1.Rows.Add(htr);
    }

    private void addNoRecordsRow()
    {
        TableRow htr = new TableRow();
        htr.Cells.Add(getCell("&nbsp;No Workitems available."));
        htr.Cells[0].ColumnSpan = 7;
        htr.Attributes.Add("Class", "Grid_Row1");
        Table1.Rows.Add(htr);
    }

    private void addRow(string Text, string Class)
    {
        TableRow htr = new TableRow();
        htr.Cells.Add(getCell("&nbsp;" + Text));
        htr.Cells[0].ColumnSpan = 7;
        htr.Attributes.Add("Class", Class);
        Table1.Rows.Add(htr);
    }

    private void getTableFromArray(ref DataTable dtHistory, DataRow[] drr)
    {
        try
        {
            foreach (DataRow dr in drr)
                dtHistory.ImportRow(dr);
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("getTableFromArray : Error :" + ex.Message);
        }
    }

    private void getpageicons() // added by srivatsan for icon display
    {
        PageIcons icons = new PageIcons();
        HDDatabase HDD = new HDDatabase();
        try
        {
            string strPagesetRowId = HDD.GetColumnValue("sy_mnu", "pageset_id", "af_row_id='" + Request.QueryString["mn"].ToString() + "'");
            imgliteral.Text = "&nbsp;" + icons.geticontextfrompageset(strPagesetRowId, 2).ToString() + "&nbsp;";

        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error :getpageicons()" + ex.Message);
            imgliteral.Text = "&nbsp;<img src=" + PageIcons.strdefaulticon.ToString() + "></img>&nbsp;";
        }
        finally
        {
            HDD = null;
            icons = null;
        }
    }

    private string queryForTitle()
    {
        string sRet = "";
        try
        {
            if (Session["user_afrowid"].ToString() == "650a333596bbf348")
            {
            }
            else
            {
                string sCurrentUserTitle = getColumnValue("sy_usr", "af_row_id", Session["user_afrowid"].ToString(), "title");
                //sCurrentUserTitle = getColumnValue("sy_org_structure", "af_row_id", sCurrentUserTitle, "title");

                sRet = " source_title in ('" + sCurrentUserTitle + "', '" + Session["user_afrowid"].ToString() + "')";
                if (sRet.Trim() != "")
                    sRet = sRet + " or ";
                sRet = sRet + " target_title in ('" + sCurrentUserTitle + "', '" + Session["user_afrowid"].ToString() + "')";
            }
        }

        catch
        {
        }

        return sRet;
    }

    private string queryForQuickSearch()
    {
        string sCondition = "";
        try
        {
            if (ddlRuleset.SelectedValue != "")
                sCondition = " ruleset_id='" + ddlRuleset.SelectedValue + "'";
            if (ddlStatus.SelectedValue != "" && ddlStatus.SelectedValue != "--All--")
            {
                if (sCondition != "")
                    sCondition += " and ";
                sCondition += "status='" + ddlStatus.SelectedValue + "'";
            }
        }

        catch
        {
        }
        return sCondition;
    }

    private string wrapItAsMessage(string msg, bool isError)
    {
        return "<div class=\"" + (isError == true ? "mbErrorMessage" : "mbMessage") + "\">" + msg + "</div>";
    }
}
